﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.BussnessLogicLayer;
using Capstone_project.DatabaseAccessLayer;

namespace Capstone_project
{
    public partial class frmViewCustomer : Form
    {
        public frmViewCustomer()
        {
            InitializeComponent();
        }
        SqlConnection con = Common.con;
        DataTable dt = new DataTable();
        SqlDataAdapter adap;

        public void frmViewCustomer_Load(object sender, EventArgs e)
        {
            btnUpdate.Visible = false;
            ViewInfo(frmSearchProject.bcd);
            txtIdCustomer.Enabled = false;
            txtNameCustomer.Enabled = false;
            txtAddress.Enabled = false;
            txtPhone.Enabled = false;
        }
        public void ViewInfo(string namecustomer)
        {
            dt.Rows.Clear();
            if(con.State!=ConnectionState.Open)
                con.Open();
            string sql = "SELECT * FROM CUSTOMER where NAME_CUSTOMER LIKE'%"+frmSearchProject.bcd+"%'";
            //WHERE (NAME_CUSTOMER like '%" + namecustomer + "%')";
            SqlCommand cmd = new SqlCommand(sql, con);
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            txtIdCustomer.Text = dt.Rows[0][0].ToString();
            txtNameCustomer.Text = dt.Rows[0][1].ToString();
            txtAddress.Text = dt.Rows[0][2].ToString();
            txtPhone.Text = dt.Rows[0][3].ToString();
            con.Close();

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            btnUpdate.Visible = true;
            txtNameCustomer.Enabled = true;
            txtAddress.Enabled = true;
            txtPhone.Enabled = true;
            btnEdit.Visible = false;
            
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtNameCustomer.Text == "" || txtAddress.Text == "" || txtPhone.Text == "")
            {
                MessageBox.Show("You must enter all infomation");
            }

            else
            {
                if(con.State!=ConnectionState.Open)
                    con.Open();
                string sql = "UPDATE CUSTOMER SET NAME_CUSTOMER='" + txtNameCustomer.Text + "',ADDRESS='" + txtAddress.Text + "',PHONE='" + txtPhone.Text + "' WHERE ID_CUSTOMER='"+txtIdCustomer.Text+"'";
                SqlCommand com = new SqlCommand(sql, con);
                int res = com.ExecuteNonQuery();
                con.Close();
                if (res != 0)
                {
                    MessageBox.Show("Customer information updated successfully !");
                    this.Close();
                }
            }
        }
    }
}
